#!/usr/bin/env python
import os
import gtk
import gobject
import pdb
from gxplanet import *

class GxplanetStatusIcon(gtk.StatusIcon):
	def __init__(self, gui):
		gtk.StatusIcon.__init__(self)
		self.gxplanet = gui
		self.showed = True
		menu = '''
			<ui>
			 <menubar name="Menubar">
			  <menu action="Menu">
			   <menuitem action="Refresh"/>
			   <menuitem action="Preferences"/>
			   <separator/>
			   <menuitem action="About"/>
			   <separator/>
			   <menuitem action="Exit"/>
			  </menu>
			 </menubar>
			</ui>
		'''
		actions = [
			('Menu',  None, 'Menu'),
			('Refresh', gtk.STOCK_REFRESH, '_Refresh', None, 'Force to Refresh the Background', self.on_refresh),
			('Preferences', gtk.STOCK_PREFERENCES, '_Preferences...', None, 'Change Xplanet preferences', self.on_preferences),
			('About', gtk.STOCK_ABOUT, '_About...', None, 'About GxPlanet', self.on_about),
			('Exit', gtk.STOCK_QUIT, '_Exit...', None, 'Close the application', self.on_exit)]
		ag = gtk.ActionGroup('Actions')
		ag.add_actions(actions)
		self.manager = gtk.UIManager()
		self.manager.insert_action_group(ag, 0)
		self.manager.add_ui_from_string(menu)
		self.menu = self.manager.get_widget('/Menubar/Menu/About').props.parent
		search = self.manager.get_widget('/Menubar/Menu/Search')
		self.set_from_file('/usr/share/pixmaps/gxplanet.png')
		self.set_tooltip('GUI Xplanet Configurator')
		self.set_visible(True)
		self.connect('activate', self.on_preferences)
		self.connect('popup-menu', self.on_popup_menu)

	def on_preferences(self, data):
		if self.gxplanet.statusGUI():
			self.gxplanet.hideGUI()
		else:
			self.gxplanet.showGUI()

	def on_refresh(self,data):
		self.gxplanet.refreshImage()

	def on_popup_menu(self, status, button, time):
		self.menu.popup(None, None, None, button, time)

	def on_about(self, data):
		dialog = gtk.AboutDialog()
		dialog.set_name('Gxplanet')
		dialog.set_version('0.5.0')
		dialog.set_comments('A GUI Configuratior Interface for Xplanet')
		dialog.set_website('http://code.google.com/p/gxplanet')
		dialog.set_authors(['Lluis Hernandez Saladie <pq2son2@gmail.com>'])
		dialog.run()
		dialog.destroy()

	def on_exit(self,data):
		self.gxplanet.exitApp()
